package com.example.wordfilter.service;

import com.example.wordfilter.properties.WordReaderProperties;
import com.example.wordfilter.util.WordReaderUtil;
import com.example.wordfilter.util.DFAfilterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

@EnableConfigurationProperties({WordReaderProperties.class})
public class DFAWordFilter {
    @Autowired
    private WordReaderProperties readerProperties;
    private final Logger logger = Logger.getLogger(DFAWordFilter.class.getName());

    @Bean
    @ConditionalOnMissingBean
    public DFAWordBean getDFAsensitiveWord(){
        boolean bool = readerProperties.getCensorWordEnable();
        DFAWordBean dfaSensitiveWordBean = new DFAWordBean();
        if(bool){
            if(readerProperties.getDfaEnabled()){
                Set<String> set = WordReaderUtil.toSet(readerProperties.getCensorWordsLocation());
                Map<String,Object> map = DFAfilterUtil.getSensitiveWordToHashMap(set);
                dfaSensitiveWordBean.setMap(map);
                logger.log(Level.INFO,"加载敏感词到map，文件加载路径：" + readerProperties.getCensorWordsLocation());
            }
        }
        return dfaSensitiveWordBean;
    }
}
